package com.ruoyi.manufacturer.common;

import com.ruoyi.base.domain.BaseDevice;
import com.ruoyi.base.domain.BaseModel;
import com.ruoyi.base.domain.BaseType;
import com.ruoyi.base.service.IBaseDeviceService;
import com.ruoyi.base.service.IBaseModelService;
import com.ruoyi.base.service.IBaseTypeService;
import com.ruoyi.common.consts.DefaultValues;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manufacturer.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 设备信息处理服务
 */
@Service
public class DeviceHandler{
    @Autowired
    protected IBaseDeviceService baseDeviceService;
    @Autowired
    protected IBaseTypeService baseTypeService;
    @Autowired
    protected IBaseModelService baseModelService;

    /**
     * 默认的注册设备流程,先判断是否存在，同步设备业务主键，新增未注册设备
     * @param device 设备
     * @param pfCode 平台代码
     * @return BaseDevice
     */
    public synchronized BaseDevice processDevice(MessageVo.Device device, String pfCode){
        BaseDevice baseDevice = null;
        if(device.getId() != null){
            baseDevice = baseDeviceService.selectBaseDeviceByExtCode(device.getId());
            if(baseDevice != null){
                return baseDevice;
            }
        }
        baseDevice = baseDeviceService.selectBaseDeviceByCode(device.getCode());
        //自动关联设备外键
        if(baseDevice != null && device.getId() != null){
            baseDevice.setExtCode(device.getId());
            baseDeviceService.updateBaseDeviceStatus(baseDevice);
        }

        baseDevice = createDevice(device.getCode(), device.getName(), device.getType(), device.getModel(), device.getId(), pfCode);
        baseDeviceService.insertBaseDevice(baseDevice);
        return baseDevice;
    }

    /**
     * 创建设备的基本信息
     * @param code 代码
     * @param name 名称
     * @param type 类型（代码）
     * @param model 型号
     * @param extCode 外部主键
     * @param pfCode 平台代码
     * @return BaseDevice
     */
    public BaseDevice createDevice(String code, String name, String type, String model, String extCode, String pfCode){
        //默认信息
        BaseDevice baseDevice = new BaseDevice();
        baseDevice.setDeptId(DefaultValues.deptId);
        baseDevice.setRunStatus(BaseDevice.RUN_NORMAL);
        baseDevice.setUseStatus(BaseDevice.USE_NORMAL);
        baseDevice.setPfCode(pfCode);
        baseDevice.setCreateBy(DefaultValues.userName);

        //基本信息
        baseDevice.setExtCode(extCode);
        baseDevice.setCode(code);
        baseDevice.setPosition(name == null ? code : name);

        //分类
        BaseType baseType = baseTypeService.selectBaseTypeByCode(type);
        if(baseType == null){
            baseType = baseTypeService.selectBaseTypeByCode(DefaultValues.deviceType);
        }
        baseDevice.setTypeId(baseType.getId());

        //型号
        if(StringUtils.isNotEmpty(model)){
            BaseModel baseModel = baseModelService.selectBaseModelByName(baseType.getId(), model);
            if(baseModel != null){
                baseDevice.setModelId(baseModel.getId());
            }
        }
        return baseDevice;
    }
}
